package controller.transform;

import javax.vecmath.AxisAngle4d;
import javax.vecmath.Vector3d;

import math.Precision;

import component.Ball;
import component.group.ComponentGroup;

public class AlignAnglePivot extends AlignAngle {

	private final double angle;
	
	public AlignAnglePivot(Gui gui, Scene scene, Ball pivot, double angle) {
		super(gui, scene, pivot);
		this.angle = angle;
	}

	@Override
	protected void tryToAlign(ComponentGroup group, Vector3d referenceVector,
			Vector3d alignVector) {
		double current = referenceVector.angle(alignVector);
		if (Precision.compare(current, Math.PI)) {
			gui.setStatusText("Unable to form shape, because angle between tubes is 90 degrees.");
			return;
		}
		if (Precision.compare(current, angle)) {
			gui.setStatusText("Already forms correct angle.");
			return;
		}
		
		Vector3d cross = new Vector3d();
		cross.cross(referenceVector, alignVector);
		System.out.println(referenceVector);
		System.out.println(alignVector);
		System.out.println(cross);
		double diff = angle - current;
		AxisAngle4d axisAngle = new AxisAngle4d(cross, diff);
		gui.setStatusText("Trying to make angle " + diff);
		this.alignGroup(group, axisAngle, connectionBall.getPos());
	}

}
